﻿using System;
using System.Collections.Generic;

using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;
using System.Data;
using System.Data.SqlClient;


public partial class BlogforAdmin : System.Web.UI.Page
{
    SqlConnection CON;
    SqlCommand CMD;
    SqlDataAdapter DA;
    DataSet DS;
    private string strconnect;

    protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
    {
        lblID.Text = GridView1.SelectedRow.Cells[0].Text;
        txtTopic.Text = GridView1.SelectedRow.Cells[1].Text;
        try
        {
            if (CON.State == ConnectionState.Closed)
                CON.Open();
            String sql = "SELECT photo FROM Blog WHERE ID =" + lblID.Text;
            SqlCommand cmd = new SqlCommand(sql, CON);
            SqlDataReader rd = cmd.ExecuteReader();
            while (rd.Read())
            {
                Image1.Visible = true;
                Image1.ImageUrl = rd[0].ToString();
            }

        }
        finally
        {
            CON.Close();
        }

    }
    protected void Page_Load(object sender, EventArgs e)
    {
        strconnect = "Server = localhost;database=photofpt;trusted_connection=true";
        CON = new SqlConnection(strconnect);
        DS = new DataSet();
        if (!IsPostBack)
            BindInfoToGridview();
    }

    protected void BindInfoToGridview()
    {
        try
        {
            if (CON.State == ConnectionState.Closed)
                CON.Open();
            CMD = new SqlCommand("demoBlog", CON);
            CMD.CommandType = CommandType.StoredProcedure;
            DA = new SqlDataAdapter(CMD);
            DA.Fill(DS);
            GridView1.DataSource = DS.Tables[0];
            GridView1.DataBind();
        }
        catch (SqlException ex)
        {
            throw new Exception(ex.Message);
        }
        finally
        {
            CON.Close();
        }
    }
    protected void btnInsert_Click(object sender, EventArgs e)
    {
        try
        {
            if (CON.State == ConnectionState.Open)
                CON.Close();
            else
                CON.Open();
            CMD = new SqlCommand("InsertBlog", CON);
            CMD.CommandType = CommandType.StoredProcedure;
            CMD.Parameters.Add("@Topic", SqlDbType.VarChar).Value = txtTopic.Text;
            string img = flUpload.FileName.ToString();
            CMD.Parameters.Add("@photo", SqlDbType.VarChar).Value = "~\\images\\" + img;

            CMD.ExecuteNonQuery();
            flUpload.SaveAs(Server.MapPath("~\\images\\" + img));
            BindInfoToGridview();
        }
        catch (SqlException ex)
        {
            throw new Exception(ex.Message);
        }
        finally
        {
            CON.Close();
        }

    }
    protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        try
        {
            DS = new DataSet();
            if (CON.State == ConnectionState.Closed)
                CON.Open();

            GridViewRow row = (GridViewRow)GridView1.Rows[e.RowIndex];
            int id = int.Parse(GridView1.DataKeys[e.RowIndex].Value.ToString());

            CMD = new SqlCommand("DeleteBlog", CON);
            CMD.Parameters.Add("@ID", SqlDbType.Int).Value = id;
            CMD.CommandType = CommandType.StoredProcedure;
            DA = new SqlDataAdapter(CMD);
            CMD.ExecuteNonQuery();
            BindInfoToGridview();

        }
        catch (SqlException ex)
        {
            throw new Exception(ex.Message);
        }
        finally
        {
            CON.Close();
        }

    }
    protected void btnUpdate_Click(object sender, EventArgs e)
    {
        //ds = new DataSet();
        if (CON.State == ConnectionState.Open)
            CON.Close();
        else
            CON.Open();

        CMD = new SqlCommand("UpdateBlog", CON);
        CMD.CommandType = CommandType.StoredProcedure;
        CMD.Parameters.Add("@ID", SqlDbType.VarChar).Value = lblID.Text.Trim();
        CMD.Parameters.Add("@Topic", SqlDbType.VarChar).Value = txtTopic.Text.Trim();
        string img = flUpload.FileName.ToString();
        CMD.Parameters.Add("@photo", SqlDbType.VarChar).Value = "~\\images\\" + img;
        CMD.ExecuteNonQuery();
        flUpload.SaveAs(Server.MapPath("~\\images\\" + img));
        GridView1.EditIndex = -1;
        BindInfoToGridview();
        CON.Close();
        txtTopic.Text = "";
        
        

    }
}